<?php
declare (strict_types=1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class PurchaseGoodsIn extends Model
{
    use SoftDelete;

    protected $deleteTime = false;
    protected $table = 'bl_purchase_goods_ins';

    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');

        //按商品id查找
        if ($goods_id = input("goods_id")) {
            $where[] = ["goods_id", "like", "%" . $goods_id . "%"];
        }
        //按入库说明查找
        if ($in_info = input("in_info")) {
            $where[] = ["in_info", "like", "%" . $in_info . "%"];
        }
        //按添加时间查找
        $start = input("get.add_time-start");
        $end = input("get.add_time-end");
        if ($start && $end) {
            $where[] = ["add_time", "between", [$start, date("Y-m-d", strtotime("$end +1 day"))]];
        }
        $admin = session('admin');
        if ($admin['school_id']) {
            $where[] = ["school_id", "=", $admin['school_id']];
        }
        if (input("is_low",-1)>=0){
            $where[] = ["is_low", "=", input("is_low")];
        }

        $list = self::with([
            'get_goods' => function($a) {
                $a->field('id,goods_name');
            },
            'get_admin' => function($a) {
                $a->field('id,username');
            }
        ])->order('id', 'desc')->with('getClass')->where($where)->paginate($limit);
//        dd($list->toArray());
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public function getGoods()
    {
        return $this->hasOne(PurchaseGoods::class, 'id', 'goods_id');
    }

    public function getAdmin()
    {
        return $this->hasOne(AdminAdmin::class, 'id', 'admin_id');
    }
    public function getClass()
    {
        return $this->hasOne(PurchaseGoodsClass::class, 'id', 'class_id');
    }
    public function getGoodsOut()
    {
        return $this->hasMany(PurchaseGoodsOuts::class, 'goods_in_id', 'id');
    }
}
